package com.carmaintain.servlets.customer;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.carmaintain.common.Constants;
import com.carmaintain.common.Utils;
import com.carmaintain.db.CustomerLib;
import com.carmaintain.db.UserLib;
import com.carmaintain.models.ResultObject;

/**
 * Servlet implementation class ModifyCarInfo
 */
public class ModifyCarInfo extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ModifyCarInfo() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		response.getWriter().print("请使用POST方式调用");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ResultObject result = new ResultObject();
		String customerId;
		String licenseNumber = "";
		String carModel = "";
		String displacement = "";
		String buyDate = "";
		String photo = "";
		
		try {
			request.setCharacterEncoding("UTF-8");
			if (!Utils.checkSign(request)) {
				result.setCode(Constants.CODE_SIGN_ERROR);
				result.setMessage(Constants.MESSAGE_WRONG_SIGN);
				response.setCharacterEncoding("UTF-8");
				response.getWriter().print(Utils.genJsonString(result));
				return;
			}
			customerId = UserLib.checkUserLogin(request.getParameter("customer_id"), request.getParameter("usercode"), Constants.USER_TYPE_CUSTOMER);
			if (customerId == null) {
				throw new Exception();
			}			
			if(Utils.empty(request.getParameter("car_license")) || Utils.empty(request.getParameter("car_model"))
					|| Utils.empty(request.getParameter("displacement")) || Utils.empty(request.getParameter("buy_date"))){
				result.setCode(Constants.CODE_MISS_PARAMATER);
				result.setMessage(Constants.MESSAGE_MISS_PARAMATER);
				response.setCharacterEncoding("UTF-8");
				response.getWriter().print(Utils.genJsonString(result));
				return;
			}else{
				licenseNumber = request.getParameter("car_license").trim();
				carModel = request.getParameter("car_model").trim();
				displacement = request.getParameter("displacement").trim();
				buyDate = request.getParameter("buy_date").trim();
			}
			if(!Utils.empty(request.getParameter("photo"))){
				photo = request.getParameter("photo").trim();
			}
		} catch (Exception e) {
			result.setCode(Constants.CODE_WRONG_PARAMATER);
			result.setMessage(Constants.MESSAGE_WRONG_PARAMATER);
			Utils.addTestMessage(result, e);
			response.setCharacterEncoding("UTF-8");
			response.getWriter().print(Utils.genJsonString(result));
			return;
		}
		
		try {
			int ret = CustomerLib.modifyCarInfo(customerId, licenseNumber, carModel, displacement, buyDate, photo);
			if (ret == Constants.CODE_SUCCESS) {
				result.setCode(Constants.CODE_SUCCESS);
				result.setMessage(Constants.MESSAGE_SUCCESS);
			} else if (ret == Constants.CODE_LICENSE_EXIST){
				result.setCode(Constants.CODE_LICENSE_EXIST);
				result.setMessage(Constants.MESSAGE_LICENSE_EXIST);
			}
		} catch (Exception e) {
			result.setCode(Constants.CODE_EXCEPTION);
			result.setMessage(Constants.MESSAGE_EXCETION);
			Utils.addTestMessage(result, e);
		}
		response.setCharacterEncoding("UTF-8");
		response.getWriter().print(Utils.genJsonString(result));
	}

}
